package com.zackeus.xupdate.entity;

import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

/**
 * 版本更新信息实体
 * @name: UpdateEntity
 * @author: zz
 * @date: 2022/12/6
 */
public class UpdateEntity implements LibraryUpdateEntity, Parcelable {

    /* 版本号 */
    private int mVersionCode;
    /* 版本名称 */
    private String mVersionName;
    /* 更新内容 */
    private String mContent;

    //============ 下载信息 ============//
    /* 下载地址 */
    private String mUrl;
    /* 下载文件的加密值，用于校验，防止下载的apk文件被替换【比如MD5】 */
    private String mEncrypt;
    /* 下载文件的大小【单位：KB】 */
    private long mSize;

    private boolean mHasUpdate;
    private boolean mForce;
    private boolean mIgnorable;

    private String mFileName;
    private String mFileDir;

    protected UpdateEntity(Parcel in) {
        mVersionCode = in.readInt();
        mVersionName = in.readString();
        mContent = in.readString();
        mUrl = in.readString();
        mEncrypt = in.readString();
        mSize = in.readLong();
        mHasUpdate = in.readByte() != 0;
        mForce = in.readByte() != 0;
        mIgnorable = in.readByte() != 0;
        mFileName = in.readString();
        mFileDir = in.readString();
    }

    public UpdateEntity(LibraryUpdateEntity libraryUpdate) {
        mVersionCode = libraryUpdate.getVersionCode();
        mVersionName = libraryUpdate.getVersionName();
        mContent = libraryUpdate.getContent();
        mUrl = libraryUpdate.getUrl();
        mEncrypt = libraryUpdate.getEncrypt();
        mSize = libraryUpdate.getSize();
        mHasUpdate = libraryUpdate.isHasUpdate();
        mForce = libraryUpdate.isForce();
        mIgnorable = libraryUpdate.isIgnorable();
        mFileName = libraryUpdate.getFileName();
        mFileDir = libraryUpdate.getFileDir();
    }

    public static final Creator<UpdateEntity> CREATOR = new Creator<UpdateEntity>() {
        @Override
        public UpdateEntity createFromParcel(Parcel in) {
            return new UpdateEntity(in);
        }

        @Override
        public UpdateEntity[] newArray(int size) {
            return new UpdateEntity[size];
        }
    };

    @Override
    public int getVersionCode() {
        return this.mVersionCode;
    }

    @Override
    public String getVersionName() {
        return this.mVersionName;
    }

    @Override
    public String getContent() {
        return this.mContent;
    }

    @Override
    public String getUrl() {
        return this.mUrl;
    }

    @Override
    public String getEncrypt() {
        return this.mEncrypt;
    }

    @Override
    public long getSize() {
        return this.mSize;
    }

    @Override
    public boolean isHasUpdate() {
        return this.mHasUpdate;
    }

    @Override
    public boolean isForce() {
        return this.mForce;
    }

    @Override
    public boolean isIgnorable() {
        return this.mIgnorable;
    }

    @NonNull
    @Override
    public String getFileName() {
        return this.mFileName;
    }

    @NonNull
    @Override
    public String getFileDir() {
        return this.mFileDir;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(mVersionCode);
        parcel.writeString(mVersionName);
        parcel.writeString(mContent);
        parcel.writeString(mUrl);
        parcel.writeString(mEncrypt);
        parcel.writeLong(mSize);
        parcel.writeByte((byte) (mHasUpdate ? 1 : 0));
        parcel.writeByte((byte) (mForce ? 1 : 0));
        parcel.writeByte((byte) (mIgnorable ? 1 : 0));
        parcel.writeString(mFileName);
        parcel.writeString(mFileDir);
    }

    @NonNull
    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }
}
